﻿//using System;
//using System.Threading;
//using System.Threading.Tasks;
//namespace Chapter1.Threads
//{
//    public class Program
//    {
//        static void Main()
//        {
//            CancellationTokenSource cancellationTokenSource =
//            new CancellationTokenSource();
//            CancellationToken token = cancellationTokenSource.Token;
//            Task task = Task.Run(() =>
//            {
//                while (!token.IsCancellationRequested)
//                {
//                    Console.Write("*");
//                    Thread.Sleep(1000);
//                }
//                token.ThrowIfCancellationRequested();
//            }, token);
//            try
//            {
//                Console.WriteLine("Press enter to stop the task");
//                Console.ReadLine();
//                cancellationTokenSource.Cancel();
//                task.Wait();
//            }
//            catch (AggregateException e)
//            {
//                Console.WriteLine(e.InnerExceptions[0].Message);
//            }
//            Console.WriteLine("Press enter to end the application");
//            Console.ReadLine();
//        }
//    }
//}
//// Displays
//// Press enter to stop the task
//// **
//// A task was canceled.
//// Press enter to end the application